package com.example.ViewPagerSum.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.ViewPagerSum.PhotoDetailActivity;
import com.example.ViewPagerSum.R;
import com.example.ViewPagerSum.adapter.PhotoGridViewAdapter;
import com.example.ViewPagerSum.domain.MmPhoto;
import com.example.ViewPagerSum.domain.ResultTask;
import com.example.ViewPagerSum.interfaces.FinishedCallBack;
import com.example.ViewPagerSum.task.PureTask;
import com.example.ViewPagerSum.util.*;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by wangjing on 2015/9/9at0:25.
 * <p/>
 * Email:wjontheway@163.com
 * <p/>
 * ViewPagerSum   清纯
 */
public class SubPureMmFragment extends Fragment implements AdapterView.OnItemClickListener, FinishedCallBack {
    private GridView  gridView;
    private  Activity mActivity;
    private List<MmPhoto> mmPhotoList;
    private PhotoGridViewAdapter adapter;
    private ProgressBar  bar;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = getActivity();

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View  view=null;
        view=inflater.inflate(R.layout.fragment_pure_mm,container,false);
        gridView= (GridView) view.findViewById(R.id.photo_list_gridview);
        bar= (ProgressBar) view.findViewById(R.id.circle_progressbar);
        gridView.setOnItemClickListener(this);
        return view;
    }


    @Override
    public void onStart() {
        //请求网络数据
        PureTask task=new PureTask(this);
        task.execute();
        super.onStart();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(mActivity, PhotoDetailActivity.class);
        MmPhoto mmPhoto = mmPhotoList.get(position);
        intent.putExtra("mmPhoto",mmPhoto);
        startActivity(intent);
    }


    /**
     * 异步任务完成后接口回调这个方法
     * @param resultTask
     */
    @Override
    public void onFinishedCallBack(ResultTask resultTask) {
        if (resultTask != null) {
            if(resultTask.action== Constants.ACTION_PURE_TASK){
                setupPure((JSONObject)resultTask.data);
                bar.setVisibility(View.INVISIBLE);
            }
        }
    }

    /**
     * 在此处解析JSON数据
     * @param data
     */
    private void setupPure(JSONObject data) {
        if (data != null) {
            ParseMmJson mParseMmJson=new ParseMmJson();
            mmPhotoList = mParseMmJson.getMmPhotoList(data);
            adapter = new PhotoGridViewAdapter(mActivity, mmPhotoList);
            gridView.setAdapter(adapter);
        }else{
            //TODO  在没有网络的时候，提示用户没有网络
        }

    }
}
